package cn.itedus.lottery.infrastructure.repository;

import cn.itedus.lottery.common.Constants;
import cn.itedus.lottery.domain.rule.model.aggregates.TreeRuleRich;
import cn.itedus.lottery.domain.rule.model.vo.TreeNodeLineVO;
import cn.itedus.lottery.domain.rule.model.vo.TreeNodeVO;
import cn.itedus.lottery.domain.rule.model.vo.TreeRootVO;
import cn.itedus.lottery.domain.rule.repository.IRuleRepository;
import cn.itedus.lottery.infrastructure.dao.IRuleTreeDao;
import cn.itedus.lottery.infrastructure.dao.IRuleTreeNodeDao;
import cn.itedus.lottery.infrastructure.dao.IRuleTreeNodeLineDao;
import cn.itedus.lottery.infrastructure.po.RuleTree;
import cn.itedus.lottery.infrastructure.po.RuleTreeNode;
import cn.itedus.lottery.infrastructure.po.RuleTreeNodeLine;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: RuleRepository
 * @Description: 规则信息仓储服务
 * @Author: szq
 * @Date: 2021-11-03 16:08:59
 */
@Repository
public class RuleRepository implements IRuleRepository {

    @Resource
    private IRuleTreeDao ruleTreeDao;
    @Resource
    private IRuleTreeNodeDao ruleTreeNodeDao;
    @Resource
    private IRuleTreeNodeLineDao ruleTreeNodeLineDao;

    @Override
    public TreeRuleRich queryTreeRuleRich(Long treeId) {
        //规则树
        RuleTree ruleTree = ruleTreeDao.queryRuleTreeByTreeId(treeId);
        TreeRootVO treeRootVO = new TreeRootVO();
        treeRootVO.setTreeId(treeId);
        treeRootVO.setTreeRootNodeId(ruleTree.getTreeRootNodeId());
        treeRootVO.setTreeName(ruleTree.getTreeName());

        //树结点，树连接线
        Map<Long, TreeNodeVO> treeNodeVOMap = new HashMap<>();
        List<RuleTreeNode> ruleTreeNodes = ruleTreeNodeDao.queryRuleTreeNodeList(treeId);
        ruleTreeNodes.stream().forEach(t->{
            List<TreeNodeLineVO> treeNodeLineInfoList = new ArrayList<>();
            if (Constants.NodeType.STEM.equals(t.getNodeType())){
                RuleTreeNodeLine ruleTreeNodeLineReq = new RuleTreeNodeLine();
                ruleTreeNodeLineReq.setTreeId(treeId);
                ruleTreeNodeLineReq.setNodeIdFrom(t.getId());
                List<RuleTreeNodeLine> ruleTreeNodeLines = ruleTreeNodeLineDao.queryRuleTreeNodeLineList(ruleTreeNodeLineReq);
                ruleTreeNodeLines.stream().forEach(r->{
                    TreeNodeLineVO treeNodeLineInfo = new TreeNodeLineVO();
                    treeNodeLineInfo.setNodeIdFrom(r.getNodeIdFrom());
                    treeNodeLineInfo.setNodeIdTo(r.getNodeIdTo());
                    treeNodeLineInfo.setRuleLimitType(r.getRuleLimitType());
                    treeNodeLineInfo.setRuleLimitValue(r.getRuleLimitValue());
                    treeNodeLineInfoList.add(treeNodeLineInfo);
                });
            }
            TreeNodeVO treeNodeInfo = new TreeNodeVO();
            treeNodeInfo.setTreeId(t.getTreeId());
            treeNodeInfo.setTreeNodeId(t.getId());
            treeNodeInfo.setNodeType(t.getNodeType());
            treeNodeInfo.setNodeValue(t.getNodeValue());
            treeNodeInfo.setRuleKey(t.getRuleKey());
            treeNodeInfo.setRuleDesc(t.getRuleDesc());
            treeNodeInfo.setTreeNodeLineInfoList(treeNodeLineInfoList);

            treeNodeVOMap.put(t.getId(), treeNodeInfo);
        });
        TreeRuleRich treeRuleRich = new TreeRuleRich();
        treeRuleRich.setTreeRoot(treeRootVO);
        treeRuleRich.setTreeNodeMap(treeNodeVOMap);
        return treeRuleRich;
    }
}
